package com.android.personal.kotlin.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.personal.kotlin.R;
import com.android.personal.kotlin.model.ListButtonModel;

import java.util.List;

import rx.Observable;
import rx.Subscriber;

/**
 * Created by zhangwentao on 2017/1/5.
 */

public class ListButtonAdapter extends RecyclerView.Adapter<ListButtonAdapter.ListButtonViewHolder> {

    private Context mContext;
    private List<ListButtonModel> mListButtonModelList;
    public ListButtonAdapter(Context context) {
        mContext = context;
    }

    public void setData(List<ListButtonModel> listButtonModels) {
        mListButtonModelList = listButtonModels;
        notifyDataSetChanged();
    }

    @Override
    public ListButtonAdapter.ListButtonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ListButtonViewHolder listButtonViewHolder = new ListButtonViewHolder(LayoutInflater.from(mContext).inflate(R.layout.list_button_item, null));
        return listButtonViewHolder;
    }

    @Override
    public void onBindViewHolder(ListButtonViewHolder holder, final int position) {
        holder.name.setText(mListButtonModelList.get(position).name);

        if (mListButtonModelList.get(position).isClick) {
            holder.play.setText("播放ing");
        } else {
            holder.play.setText("播放");
        }

        holder.play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeData(position);
            }
        });

        holder.pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListButtonModelList.get(position).isClick) {
                    Toast.makeText(mContext, "暂停", Toast.LENGTH_SHORT).show();
                }
            }
        });

        holder.stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListButtonModelList.get(position).isClick) {
                    Toast.makeText(mContext, "停止", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mListButtonModelList == null ? 0 : mListButtonModelList.size();
    }

    private void changeData(final int i) {
        Observable.from(mListButtonModelList).subscribe(new Subscriber<ListButtonModel>() {
            @Override
            public void onCompleted() {
                mListButtonModelList.get(i).isClick = true;
                notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(ListButtonModel listButtonModel) {
                listButtonModel.isClick = false;
            }
        });
    }

    class ListButtonViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        Button play;
        Button pause;
        Button stop;
        public ListButtonViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            play = (Button) itemView.findViewById(R.id.play);
            pause = (Button) itemView.findViewById(R.id.pause);
            stop = (Button) itemView.findViewById(R.id.stop);
        }
    }
}
